{include $data.pathToError}
<script type="text/javascript">
    {literal}
        
    function sendAction(action){
        $("#action").val(action);
        $("#adding").submit();    
    }
        
    function changeOriginalAmount(tariff, amount) {
        //ramount_original
        if (tariff==6){
            $('#ramount_original_edit').val("0");
        } else {
            $('#ramount_original_edit').val(amount);
        }
    }
        
    function changeTariffs(value, target){
        if (typeof(target) == 'undefined')
            target = 'rtariff';
        
        if (value!=-1){
            
            var data = {
                action : 'change_tariffs',
                params: {
                    type: value
                }
            }
                
            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    result = false;
                } else {
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    $("#"+target).append(data.options)
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    function fillPrice(value, target){
        if (typeof(target) == 'undefined')
            target = 'rprice';
        if (value!=-1){
            
            var data = {
                action : 'fill_price',
                params: {
                    tariff: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).val("0.00");
                    result = false;
                } else {
                    $("#"+target).val(data.price);
                    makeFloat(".double",2);
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    $(document).ready(function() {
        $("input[type='text']").keyup(function(){
            count_tsdr_description_length($(this), '{/literal}{$smarty.const.TSDR_DESCRIPTION_MAX_LENGTH}{literal}');
        });
        $(".float").change(function() {
            makeFloat(".float",1);
        });
        $("#rdate_edit").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $(".double").change(function() {
            makeFloat(".double",2);
        });
    {/literal}
    {if isset($smarty.request.edit)}
        {literal}
            unexpended($('#rdate_edit').val(), $('#rclient_edit').val());
        {/literal}
    {/if}
    {literal}
    });
    
    {/literal}
</script>

<h2 class="pageTitle">TSDR info {if $data.record_data.locked!=0}<img height="16" title="locked" width="16" src="{$smarty.const.PATH_TO_IMAGES}locked.png" />{/if}</h2>

<form action="{$data.root}?input=tsdr_more&tsdr_id={$smarty.request.tsdr_id}" method="post" onsubmit="return checkAdd_record(
                                                                    $('#rdate_edit').val(), 
                                                                    $('#rclient_edit').val(),
                                                                    $('#rtype_edit').val(),
                                                                    $('#ramount_edit').val(),
                                                                    $('#rprice_edit').val(),
                                                                    $('#rdescription_edit').val(),
                                                                    $('#ramount_original_edit').val(),
                                                                    $('#rtariff_edit').val()
                                                                                   )">
    <table class="inlineForm">
        <tr>
            <td><b><label for="rdate">Date*</label></b></td>
            <td><b><label for="rclient">Client*</label></b></td>
            <td><b><label for="rtype">Type of record*</label></b></td>
            <td><b><label for="ramount">Amount*</label></b></td>
            <td><b><label for="rtariff">Tariff</label></b></td>
            <td><b><label for="rprice">Unit price*</label></b></td>
            <td><b><label for="rclient">Added by*</label></b></td>
            <td><b><label for="rdescription">Description*</label></b></td>
            <td></td>
        </tr>
        <tr>
            {if isset($smarty.request.edit) && $smarty.request.edit==$data.record_data.recordId && $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $data.record_data.locked==0}
                <td>
                    <input style="width: 80px;" name="record[date]" id="rdate_edit" type="text" value="{$data.record_data.date_formated}" onchange="unexpended($('#rdate_edit').val(), $('#rclient_edit').val())" />
                </td>
                <td>
                    <select name="record[client]" id="rclient_edit" size="1" onchange="unexpended($('#rdate_edit').val(), $('#rclient_edit').val())">
                        <option value="-1">Please select</option>
                        {foreach $data.clients as $key => $client}
                            <option {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $client.bad_payer==1}class="badPayer"{/if} {if $data.record_data.client==$client.klient_id}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <select name="record[type]" id="rtype_edit" size="1" onchange="changeTariffs(this.value,'rtariff_edit')">
                        <option value="-1">Please select</option>
                        {foreach $data.rtypes as $key => $type}
                            <option {if $type.Id==$data.record_data.type}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <input style="width: 50px;" name="record[amount]" id="ramount_edit" class="float" type="text" value="{$data.record_data.amount}"  />
                    {if $data.record_data.tariff == 6}
                        <input id="ramount_original_edit" type="hidden" value="{$data.record_data}"  />
                    {else}
                        <input id="ramount_original_edit" type="hidden" value="0"  />
                    {/if}
                </td>
                <td>
                    <select name="record[tariff]" id="rtariff_edit" size="1" onchange="fillPrice(this.value,'rprice_edit'); fillDescription('#rdescription_edit', this.value);changeOriginalAmount(this.value,'{$record.amount}')">
                        <option value="-1">Please select</option>
                        {foreach $data.rtariffs_edit as $key => $tariff}
                            <option {if $tariff.Id==$data.record_data.tariff}selected="selected"{/if} value="{$tariff.Id}">{$tariff.title}</option>
                        {/foreach}
                    </select>
                </td>            
                <td>
                    <input style="width: 70px;" name="record[price]" id="rprice_edit" class="double" type="text" value="{$data.record_data.unit_price}" />
                </td>
                <td>
                    <select name="record[user_id]" id="ruser_id_edit" size="1" {*onchange="showReport(this.value)"*}>
                        {foreach $data.rusers as $key => $user}
                            <option {if $data.record_data.user_id==$user.users_id}selected="selected"{/if} value="{$user.users_id}">{$user.name} {$user.surname}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <input maxlength="{$smarty.const.TSDR_DESCRIPTION_MAX_LENGTH}" style="width: 300px;" name="record[description]" id="rdescription_edit" type="text" value="{$data.record_data.description}"/>
                </td>
                <td>
                    <input name="recordId" value="{$data.record_data.recordId}" type="hidden" />
                    <input name="record_taskId" value="{$data.record_data.taskId}" type="hidden" />
                    <input name="action" value="edit_record" type="hidden" />
                    <input type="submit" value="Save" name="add_record" />
                </td>
            {else}
                <td>{$data.record_data.date_formated}</td>
                <td>{$data.record_data.client_info.fullName}</td>
                <td>{$data.record_data.type_info.type_of_record}</td>
                <td>{$data.record_data.amount}</td>
                <td>{$data.record_data.tariff_info.title}</td>
                <td>{$data.record_data.unit_price}</td>
                <td>{$data.record_data.user_info.name} {$data.record_data.user_info.surname}</td>
                <td>
                    {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT}
                        {if $data.record_data.locked!=0}
                            {$data.record_data.description}
                            <img height="16" title="locked" width="16" src="{$smarty.const.PATH_TO_IMAGES}locked.png" />
                        {else}
                            <a style="color: black" href="{$data.root}?input=tsdr_more&tsdr_id={$smarty.request.tsdr_id}&edit={$smarty.request.tsdr_id}">{$data.record_data.description}</a>
                        {/if}
                    {else}
                        {$data.record_data.description}
                    {/if}
                </td>
                <td></td>
            {/if}        
        </tr>
    </table>
</form>

<h2 class="pageTitle">Additionally attributes {if $data.record_data.locked!=0}<img height="16" title="locked" width="16" src="{$smarty.const.PATH_TO_IMAGES}locked.png" />{/if}</h2>    


<form class="adding" id="adding" action="{$data.root}?input=tsdr_more&tsdr_id={$data.record_data.recordId}" method="post">
    <table class="tableForm">
        {if $smarty.session.user.user_type_id > $smarty.const.USER_TYPE_CLIENT}
            <tr>
                <td colspan="2">
                    {if $data.record_data.locked==0}
                        <a href="javascript: deleteRecord({$data.record_data.recordId})" style="color: black; text-decoration: none">
                            <img height="16" width="16" src="{$smarty.const.PATH_TO_IMAGES}delete.png" /> Delete record
                        </a>
                    {else}
                        <img height="16" width="16" src="{$smarty.const.PATH_TO_IMAGES}delete.png" />
                        <img height="16" title="locked" width="16" src="{$smarty.const.PATH_TO_IMAGES}locked.png" /> Delete record
                    {/if}
                </td>
            </tr>
            <tr>
                <td>
                    Connected with task:
                </td>
                <td>
                    <select name="record_data[taskId]" size="1">
                        <option value="0">No connect</option>
                        {foreach $data.tasks as $key => $task}
                            <option value="{$task.taskId}" {if $task.taskId==$data.record_data.taskId}selected="selected"{/if}>{$task.title}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td>Attention</td>
                <td>
                    <input name="record_data[attention]" value="0" type="hidden" />
                    <input name="record_data[attention]" value="1" type="checkbox" {if $data.record_data.attention==1}checked="checked"{/if} />
                </td>
            </tr>
        {/if}
        <tr>
            <td colspan="2">
                {include "include/phorum.tpl"}
            </td>
        </tr>
        <tr>
            <td colspan="2"><b>New post</b></td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea name="post" cols="120" rows="10"></textarea>
            </td>
        </tr>
        <tr class="submitRow">
            <td>
                <input name="action" value="saveMore" type="hidden" id="action" />
            </td>
            <td>
                <input type="button" value="Save" name="add_task" onclick="sendAction('saveMore')" /> &nbsp; 
                <input type="button" value="Save & Send" name="add_task" onclick="sendAction('saveMore_send')" /> &nbsp; 
            </td>
        </tr>
    </table>
</form>